﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;

namespace ExpertMIS.UserControl
{
    public partial class ucRecommendExperts : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshRecommendExperts();
        }

        private void RefreshRecommendExperts()
        {
            ExpertBroker broker = new ExpertBroker();

            int count = 0;
            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue("recommendexpertcount"), out count))
            {
                count = 10;
            }

            if (LoadAsDomainExpert)
            {
                repExperts.DataSource = broker.GetExpertsByDomain(DomainOID);
                repExperts.DataBind();
            }
            else
            {
                repExperts.DataSource = broker.GetHotNExperts(count);
                repExperts.DataBind();
            }
        }

        public bool LoadAsDomainExpert
        {
            get;
            set;
        }

        public string DomainOID
        {
            get;
            set;
        }

        public override string ClientID
        {
            get
            {
                return pnRecommendExperts.ClientID;
            }
        }

        public string CSS
        {
            get
            {
                return pnRecommendExperts.CssClass;
            }
            set
            {
                pnRecommendExperts.CssClass = value;
            }
        }

        protected void repExperts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ExpertEntity entity = e.Item.DataItem as ExpertEntity;
                ucExpertShort ucShort = e.Item.FindControl("ucExpertShort1") as ucExpertShort;
                if (entity != null && ucShort != null)
                {
                    ucShort.Expert = entity;
                }
            }
        }
    }
}